package xyz.cerberu.cet.anime.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import xyz.cerberu.cet.anime.dao.AnimeDao;
import xyz.cerberu.cet.anime.domain.Anime;
import xyz.cerberu.cet.anime.exception.AnimeNotFoundException;
import xyz.cerberu.cet.global.utils.TimeUtils;

import java.sql.Date;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;

/**
 * @author β世界
 * Created on 15:32
 * @Description
 */
@Service
public class AnimeServiceImpl {
    private final AnimeDao animeDao;

    @Autowired
    public AnimeServiceImpl(AnimeDao animeDao) {
        this.animeDao = animeDao;
    }

    /**
     * @author β世界 on 18:44 2020/4/9
     * @Description 获取某一天会更新的动漫
     * @param date:日期
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    @Cacheable(value = "anime",unless = "#result.size() == 0")
    public List<Anime> sameDayAnime(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return animeDao.selectAnimeByDate(
                localDate.getYear(),
                TimeUtils.calculateQuarter(localDate.getMonthValue()),
                localDate.getMonthValue()
                );
    }

    /**
     * @author β世界 on 19:37 2020/4/26
     * @Description 获取一个年度的所有动漫
     * @param year:年度
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    @Cacheable(value = "anime",unless = "#result.size() == 0")
    public List<Anime> sameYearAnime(int year) {
        return animeDao.selectAnimeByDate(year,null,0);
    }

    /**
     * @author β世界 on 12:55 2020/7/18
     * 获取一年中某个季度的动漫
     * @param year: 年份
     * @param quarter: 季度
     * @return java.util.List<com.Cet.anime.domain.Anime>
     **/
    @Cacheable(value = "anime",key = "T(String).valueOf(#root.methodName).concat('-').concat(#year).concat('-').concat(#quarter)")
    public List<Anime> sameYearAndQuarterAnime(int year, String quarter) {
        return animeDao.selectAnimeByDate(year,quarter,0);
    }

    /**
     * @author β世界 on 20:36 2020/4/20
     * @Description 获取一个动漫的基本信息
     **/
    @Cacheable(value = "anime",key = "#aniId" ,unless = "#result == null")
    public Anime animeAbstract(String aniId) {
        Anime anime = animeDao.selectAnimeByID(aniId);
        if(anime == null){
            // 抛出动漫不存在的异常
            throw new AnimeNotFoundException("动漫ID："+aniId+" 动漫不存在");
        }
        return anime;
    }

    /**
     * @author β世界 on 9:00 2021/3/15
     * 获取今天的动漫更新情况
     * 如果设置一些特殊更新的动漫，我们也需要添加上去
     * @return java.util.List<xyz.cerberu.cet.anime.domain.Anime>
     **/
    @Cacheable(value = "anime",key = "#root.methodName",unless = "#result.size() == 0")
    public List<Anime> getNowAnimeUpdate() {
        LocalDate localDate = LocalDate.now();
        return animeDao.selectAnimeByDate(
                localDate.getYear(),
                TimeUtils.calculateQuarter(localDate.getMonthValue()),
                localDate.getDayOfWeek().getValue()
        );
    }

}
